<?php


namespace App\Repositories\Ota\Ctrip\Policy\Eloquent;


use App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface;
use App\Repositories\Policy\Eloquent\PolicyRepository;

class CtripRulePolicyRepository extends PolicyRepository implements CtripRulePolicyInterface
{

    /**
     * 政策详细信息查询请求
     * @param array $param
     * @return mixed
     */
    public function detailSearch($param = []){

        $this->MessageBody['PolicyIDList'] = $param;
        return $this->ruleReq('RulePolicyDetailSearch',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 政策详细信息查询请求
     * @param array $param
     * @return mixed
     */
    public function detailMhSearch($param = []){

        $this->MessageBody['PolicyIDList'] = $param;
        return $this->ruleReq('RulePolicyDetailSearch',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody),1);
    }

    /**
     * 添加政策
     * @param $data
     * @return mixed
     */
    public function add($data)
    {

        $this->formatData($data);
        return $this->ruleReq('RulePolicyAdd',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 添加政策
     * @param $data
     * @return mixed
     */
    public function addMh($data)
    {

        $this->formatData($data);
        return $this->ruleReq('RulePolicyAdd',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策删除请求
     * @param $param
     * @param string $type
     * @param array $lists
     * @return mixed
     */
    public function delete($param, $type = 'ByID', $isFuzzy = 'T')
    {
        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
           /* "PolicyCodeList"=>$param,*/
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];

        if ($type == 'ByID') {
            $this->MessageBody['PolicyIDList'] = $param;
        } else {

            $this->MessageBody['PolicyCodeList'] = $param;
        }

        return $this->req('RulePolicyDelete',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));

    }

    /**
     * 政策删除请求
     * @param $param
     * @param string $type
     * @param array $lists
     * @return mixed
     */
    public function deleteMh($param, $type = 'ByID', $isFuzzy = 'T')
    {
        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
            /* "PolicyCodeList"=>$param,*/
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];

        if ($type == 'ByID') {
            $this->MessageBody['PolicyIDList'] = $param;
        } else {

            $this->MessageBody['PolicyCodeList'] = $param;
        }

        return $this->req('RulePolicyDelete',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));

    }

    /**
     * 添加规则运价
     * @param $data
     * @return mixed
     */
    public function addZh($data){

        $this->formatData($data);
        return $this->ruleReq('RulePolicyAdd',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody));

    }

    /**
     * 特殊政策删除请求
     * @param $param
     * @param string $type
     * @param array $lists
     * @return mixed
     */
    public function deleteZh($param,$type='ByID',$isFuzzy='F'){

        $this->MessageBody =  [
            "DeleteType"=>$type,
            /*"PolicyIDList"=>$param,*/
            "PolicyCodeList"=>$param,
            /*"ExternalIDList"=>[
                0
            ],*/
            "PolicySource"=>"ALL",
            "IsFuzzyMatch"=>$isFuzzy
        ];
        return $this->req('RulePolicyDelete',array('MessageHead'=>$this->MessageHead,'MessageBody'=>$this->MessageBody));

    }


    /**
     * 特殊政策修改请求
     * @param $param
     * @return mixed
     */
    public function change($param)
    {

        $this->formatData($param);
        return $this->req('RulePolicyChange',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 特殊政策修改请求
     * @param $param
     * @return mixed
     */
    public function changeMh($param)
    {

        $this->formatData($param);
        return $this->req('RulePolicyChange',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }


    /**
     * 修改政策价格
     * @param $param
     * @return mixed
     */
    public function changePrice($param){

        if (count($param,1) == count($param)) {

            $param = [$param];
        }

        $PolicyList = [];
        foreach ($param as $key=>$value) {

            $policy = [
                "BasicUnit" => [
                    'BatchNo'                                   => '',
                    'TicketType'                                => '0001',
                    'IsBooking'                                 => !empty($value['IsBooking']) ? $value['IsBooking'] : 'F',
                    'OfficeNo'                                  => !empty($value['OfficeNo']) ? $value['OfficeNo'] : 'CGO134',
                    'IsSecurity'                                => 'F',
                    'NeedPata'                                  => 'F',
                    'PolicyCode'                                => 'TEST',
                    'VipCode'                                   => '',
                    'PolicyStatus'                              => env('POLICY_VALID') == 0  ? 'INVALID' : $value['PolicyStatus'],
                    'IsAutoTicket'                              => 'F',
                    'IsCustomerPoint'                           => 'F',
                    'IsShareFlight'                             => !empty($value['IsShareFlight']) ? $value['IsShareFlight'] : 'F',
                    'TicketRemark'                              => '',
                    'CarrierAirlines'                           => '',
                    'PataCode'                                  => '',
                    'IATANo'                                    => '',
                    'ProductType'                               => 0,
                ],
            ];

            $policy[ 'PolicyID'] = $value['PolicyID'];

            $PolicyList[] = $policy;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];

        return $this->req('RulePolicyChange',array('MessageHead'=>$this->MessageHeadHB,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 修改政策价格
     * @param $param
     * @return mixed
     */
    public function changeMhPrice($param){

        if (count($param,1) == count($param)) {

            $param = [$param];
        }

        $PolicyList = [];
        foreach ($param as $key=>$value) {

            $policy = [
                "BasicUnit" => [
                    'BatchNo'                                   => '',
                    'TicketType'                                => '0001',
                    'IsBooking'                                 => !empty($value['IsBooking']) ? $value['IsBooking'] : 'F',
                    'OfficeNo'                                  => !empty($value['OfficeNo']) ? $value['OfficeNo'] : 'CGO134',
                    'IsSecurity'                                => 'F',
                    'NeedPata'                                  => 'F',
                    'PolicyCode'                                => 'TEST',
                    'VipCode'                                   => '',
                    'PolicyStatus'                              => env('POLICY_VALID') == 0  ? 'INVALID' : $value['PolicyStatus'],
                    'IsAutoTicket'                              => 'F',
                    'IsCustomerPoint'                           => 'F',
                    'IsShareFlight'                             => !empty($value['IsShareFlight']) ? $value['IsShareFlight'] : 'F',
                    'TicketRemark'                              => '',
                    'CarrierAirlines'                           => '',
                    'PataCode'                                  => '',
                    'IATANo'                                    => '',
                    'ProductType'                               => 0,
                ],
            ];

            $policy[ 'PolicyID'] = $value['PolicyID'];

            $PolicyList[] = $policy;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];

        return $this->req('RulePolicyChange',array('MessageHead'=>$this->MessageHeadMh,'MessageBody'=>$this->MessageBody));
    }

    /**
     * 格式化数据
     * @param $param
     * @return array
     */
    private function formatData($param){

        $PolicyList = [];
        foreach ($param as $key=>$value) {


            $policyItem = [
                'PolicyID'  => !empty($param[0]['PolicyID']) ? $param[0]['PolicyID'] : 0,
                'BasicUnit' => [
                    'BatchNo'                                   => '',
                    'TicketType'                                => '0001',
                    'IsBooking'                                 => !empty($value['IsBooking']) ? $value['IsBooking'] : 'F',
                    'IsShortageFilter'                          => !empty($value['IsShortageFilter']) ? $value['IsShortageFilter'] : 'F',
                    'ShortageFilterCount'                       => !empty($value['ShortageFilterCount']) ? $value['ShortageFilterCount'] : 0,
                    'OfficeNo'                                  => !empty($value['OfficeNo']) ? $value['OfficeNo'] : 'CGO134',
                    'NeedPata'                                  => !empty($value['NeedPata']) ? $value['NeedPata'] : 'F',
                    'UpFloatPrice'                              => !empty($value['UpFloatPrice']) ? $value['UpFloatPrice'] : 0,
                    'DownFloatPrice'                            => !empty($value['DownFloatPrice']) ? $value['DownFloatPrice'] : 0,
                    'IsSecurity'                                => 'F',
                    'PolicyCode'                                => $value['PolicyCode'],
                    'VipCode'                                   => '',
                    'PolicyStatus'                              => env('POLICY_VALID') == 0  ? 'INVALID' : $value['PolicyStatus'],
                    'IsAutoTicket'                              => 'F',
                    'IsCustomerPoint'                           => 'F',
                    'IsShareFlight'                             => !empty($value['IsShareFlight']) ? $value['IsShareFlight'] : 'F',
                    'TicketRemark'                              => !empty($value['TicketRemark']) ? $value['TicketRemark'] : '',
                    'CarrierAirlines'                           => '',
                    'PataCode'                                  => '',
                    'IATANo'                                    => !empty($value['IATANo']) ? $value['IATANo'] : '',
                    'ProductType'                               => $value['ProductType'],
                ],

                'FlightUnit' => [
                    "FlightWay"                                 => !empty($value['FlightWay']) ? $value['FlightWay'] : "S",
                    "AirLine"                                   => $value['AirLine'],
                    "GoFlight"  => [

                        "EffectDate"        => '/Date('.strtotime($value['GoEffectDate']).'000+0800)/',
                        "ExpiryDate"        => '/Date('.strtotime($value['GoExpiryDate']).'000+0800)/',
                        'ExcludeRouteDate'  => !empty($value['GoExcludeRouteDate']) ? $value['GoExcludeRouteDate'] : NULL,
                        'DepartTimeLimit'   => !empty($value['GoDepartTimeLimit']) ? $value['GoDepartTimeLimit']: NULL,
                        "Days"              => !empty($value['GoDays']) ? implode("",$value['GoDays']) : '1234567',
                    ],
                ],
                'SaleLimitUnit'=>[
                    'EffectDate'            => $value['SaleEffectDate'],
                    'ExpiryDate'            => $value['SaleExpiryDate'],
                    'ApplyChild'            => !empty($value['ApplyChild']) ? $value['ApplyChild'] : 'F',
                    'MinAdvanceDays'        => !empty($value['AdvanceDay']) && strpos(' '.$value['AdvanceDay'],'-') ? explode('-',$value['AdvanceDay'])[0] : NULL,
                    'MaxAdvanceDays'        => !empty($value['AdvanceDay']) && strpos(' '.$value['AdvanceDay'],'-') ? explode('-',$value['AdvanceDay'])[1] : NULL,
                    'MinStopDays'           => !empty($value['StopDay']) && strpos(' '.$value['StopDay'],'-') ? explode('-',$value['StopDay'])[0] : NULL,
                    'MaxStopDays'           => !empty($value['StopDay']) && strpos(' '.$value['StopDay'],'-') ? explode('-',$value['StopDay'])[1] : NULL,
                    'ControlTime'           => !empty($value['ControlTime']) ? $value['ControlTime'] : NULL,
                ],
            ];

            //销售价格范围
            if (!empty($value['SalePriceMin']) && !empty($value['SalePriceMax'])) {

                $policyItem['SaleLimitUnit']['SalePriceMin'] = $value['SalePriceMin'];
                $policyItem['SaleLimitUnit']['SalePriceMax'] = $value['SalePriceMax'];
            }

            //运价信息处理
            foreach ($value['RulePriceList'] as &$ruleV) {

                if (!empty($ruleV['RcID'])) {

                    unset($ruleV['RuleRestrict']);
                }

            }
            $policyItem['RulePriceUnit']['RulePriceList'] = $value['RulePriceList'];

            //去程处理
            if (!empty($value['SaleFlightList'])) {

                $SaleFlightList = null;
                foreach ($value['SaleFlightList'] as $SaleFlightItem) {
                    $SaleFlightList[] = $SaleFlightItem;
                }

                $policyItem['FlightUnit']['GoFlight']['SaleFlightList'] = $SaleFlightList;
            }

            //返程处理
            if ($value['FlightWay'] == 'D') {

                $BackSaleFlightList = null;
                if (!empty($value['BackSaleFlightList'])) {

                    foreach ($value['BackSaleFlightList'] as $SaleFlightItem) {

                        $BackSaleFlightList[] = $SaleFlightItem;
                    }
                }

                $policyItem['FlightUnit']['BackFlight'] = [
                    "SaleFlightList"=>  $BackSaleFlightList,
                    "EffectDate"    => '/Date('.strtotime($value['BackEffectDate']).'000+0800)/',
                    "ExpiryDate"    => '/Date('.strtotime($value['BackExpiryDate']).'000+0800)/',
                    'ExcludeRouteDate'  => $value['BackExcludeRouteDate'],
                    'DepartTimeLimit'   => $value['BackDepartTimeLimit'],
                    "Days"              => implode("",$value['BackDays'])
                ];
            }

            $PolicyList[] = $policyItem;
        }

        $this->MessageBody = [
            "PolicyList"=>$PolicyList
        ];


    }
}